{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 315,
   "id": "4fd615da",
   "metadata": {},
   "outputs": [],
   "source": [
    "import selenium\n",
    "import hashlib\n",
    "import base64\n",
    "import json\n",
    "import requests\n",
    "import os\n",
    "from PIL import Image\n",
    "from time import sleep\n",
    "from selenium.webdriver.common.action_chains import ActionChains"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af56d8d6",
   "metadata": {},
   "source": [
    "# 设置浏览器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 316,
   "id": "a75cf586",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-316-1a9391fe1382>:22: DeprecationWarning: use options instead of chrome_options\n",
      "  driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n",
    "\n",
    "\n",
    "## 确定pdf文件的下载指定路径\n",
    "\n",
    "\n",
    "opts = webdriver.ChromeOptions()\n",
    "opts.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错\n",
    "opts.add_argument('window-size=1920x3000') #指定浏览器分辨率\n",
    "opts.add_argument('--disable-gpu') #谷歌文档提到需要加上一这个属性来规避bug\n",
    "opts.add_argument('--hide-scrollbars') #隐藏滚动条, 应对些特殊页面\n",
    "out_path = r\"C:\\Users\\admin\\Desktop\\数据挖掘\\知网pdf\" # 是你想指定的路径\n",
    "prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': out_path}\n",
    "opts.add_experimental_option('prefs', prefs)\n",
    "#opts.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度\n",
    "#opts.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败\n",
    "# opts.binary_location = \"C:\\portable\\PortableApps\\IronPortable\\App\\Iron\\chrome.exe\"\n",
    "#opts.binary_location = \"/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --version\" #\"H:\\_coding_\\Gitee\\InternetNewMedia\\CapstonePrj2016\\chromedriver.exe\"  \n",
    "\n",
    "# \"H:\\_coding_\\Gitee\\InternetNewMedia\\CapstonePrj2016\\chromedriver.exe\"  \n",
    "driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f31c24e9",
   "metadata": {},
   "source": [
    "# 登陆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "id": "881013fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get(\"https://www.cnki.net/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5115bda6",
   "metadata": {},
   "source": [
    "# 检查登录状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "id": "94b0dea8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中山大学南...'"
      ]
     },
     "execution_count": 318,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#登录\n",
    "driver.find_element_by_id('Ecp_loginShowName1').get_attribute('innerHTML')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07ea5ec9",
   "metadata": {},
   "source": [
    "# 进入高级检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "id": "f13a3379",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "高级检索\n"
     ]
    }
   ],
   "source": [
    "#高级检索\n",
    "element = driver.find_element_by_id('highSearch')\n",
    "print(element.get_attribute('innerHTML'))\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b45a495",
   "metadata": {},
   "source": [
    "# 检查窗口位置信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 320,
   "id": "2724fd39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'CDwindow-103ECDE6B6EBDEDB6712BA31227094ED'"
      ]
     },
     "execution_count": 320,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##当前窗口信息\n",
    "driver.current_window_handle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 321,
   "id": "9412cf88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['CDwindow-103ECDE6B6EBDEDB6712BA31227094ED',\n",
       " 'CDwindow-EE0F9FF198F7A8DF0100CE62BC64DCD4']"
      ]
     },
     "execution_count": 321,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##所有窗口信息\n",
    "driver.window_handles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 322,
   "id": "082084f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-322-2c997ac77236>:2: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[1])\n"
     ]
    }
   ],
   "source": [
    "#切换窗口\n",
    "driver.switch_to_window(driver.window_handles[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fb0a2b8",
   "metadata": {},
   "source": [
    "# 点击学术期刊"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "id": "831c99e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<span>学术期刊</span><em></em>\n"
     ]
    }
   ],
   "source": [
    "element = driver.find_element_by_xpath('//ul[@class=\"doctype-menus keji\"]/li[@data-id=\"xsqk\"]/a')\n",
    "print(element.get_attribute('innerHTML'))\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4bfcc0a",
   "metadata": {},
   "source": [
    "# 点击专业检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "id": "e46f5099",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "专业检索\n"
     ]
    }
   ],
   "source": [
    "##点击专业检索\n",
    "element = driver.find_element_by_name('majorSearch')\n",
    "print(element.get_attribute('innerHTML'))\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bc68f09",
   "metadata": {},
   "source": [
    "# 选择期刊来源"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 325,
   "id": "e2fcdfea",
   "metadata": {},
   "outputs": [],
   "source": [
    "#点击\"SCI\"来源\n",
    "element = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div[1]/div[3]/div/label[2]/input')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 326,
   "id": "175b98d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#点击\"EI\"来源\n",
    "element = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div[1]/div[3]/div/label[3]/input')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "id": "2e3a95a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#点击\"CSSCI\"\n",
    "element = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div[1]/div[3]/div/label[5]/input')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de44bfab",
   "metadata": {},
   "source": [
    "# 设置搜索的query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 328,
   "id": "4e678364",
   "metadata": {},
   "outputs": [],
   "source": [
    "query= 'SU = \"互联网\" '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 329,
   "id": "8bab84bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//textarea')\n",
    "element.clear()\n",
    "element.send_keys(query)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f9b126f",
   "metadata": {},
   "source": [
    "# 检查检索信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 330,
   "id": "4bf869e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//input[@value=\"检索\"]')\n",
    "element.get_attribute('innerHTML')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "id": "85c11867",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 检索数量\n",
    "driver.find_element_by_xpath('//i[@class=\"icon icon-sort\"]').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 335,
   "id": "7504d4f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 设置五十条\n",
    "driver.find_element_by_xpath('//*[@id=\"perPageDiv\"]/ul/li[3]/a').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "571d9c6b",
   "metadata": {},
   "source": [
    "# 下载PDF文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 336,
   "id": "2b7aec63",
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.switch_to.window(driver.window_handles[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "id": "4fa1d1b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 页面切换\n",
    "#封装切换页面函数，当下载完文章后可以切换回详情页面\n",
    "def qiehuan_详情():\n",
    "    all_window=driver.window_handles\n",
    "    new_page = len(all_window)\n",
    "    driver.switch_to.window(driver.window_handles[new_page-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "id": "60a11583",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 封装验证验证码是否存在的函数\n",
    "def yzm_real():\n",
    "    try:\n",
    "        driver.switch_to.window(driver.window_handles[3])\n",
    "        driver.find_element_by_xpath('//*[@id=\"vImg\"]')#验证码图片\n",
    "        return True#为真，即有验证码图片存在\n",
    "    except:\n",
    "        return False#反之为空则没有验证码，返回False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "id": "8ffd1e12",
   "metadata": {},
   "outputs": [],
   "source": [
    "#封装讯飞请求头部函数\n",
    "def getHeader():\n",
    "    curTime = str(int(time.time()))\n",
    "    param = {\"language\": \"cn|en\", \"location\": \"false\"}\n",
    "    param = json.dumps(param)\n",
    "    paramBase64 = base64.b64encode(param.encode('utf-8'))\n",
    "    m2 = hashlib.md5()\n",
    "    str1 = API_KEY + curTime + str(paramBase64,'utf-8')\n",
    "    m2.update(str1.encode('utf-8'))\n",
    "    checkSum = m2.hexdigest()\n",
    "    header = {\n",
    "        'X-CurTime': curTime,\n",
    "        'X-Param': paramBase64,\n",
    "        'X-Appid': APPID,\n",
    "        'X-CheckSum': checkSum,\n",
    "        'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',\n",
    "    }\n",
    "    return header"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 340,
   "id": "b44e3c43",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 检查出现验证码前下载的文件数量\n",
    "def wenjian1():\n",
    "    path = r\"C:\\Users\\admin\\Desktop\\数据挖掘\\知网pdf\"#（需在selenium浏览器更改下载文件存储的地址）\n",
    "    file = int(len([lists for lists in os.listdir(path) if os.path.isfile(os.path.join(path, lists))]))#通过len和int将验证码输入前的文件夹里的文件数量进行检测，获取数量为未输入验证码的文件量，需调用os模块\n",
    "    return file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 341,
   "id": "c902dcc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#检测点击提交验证码后文件的数量\n",
    "def wenjian2():\n",
    "    path = r\"C:\\Users\\admin\\Desktop\\数据挖掘\\知网pdf\"#（需在selenium浏览器更改下载文件存储的地址）\n",
    "    file = int(len([lists for lists in os.listdir(path) if os.path.isfile(os.path.join(path, lists))]))#通过len和int将验证码输入前的文件夹里的文件数量进行检测，获取数量为未输入验证码的文件量，需调用os模块\n",
    "    return file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 342,
   "id": "d6f03a15",
   "metadata": {},
   "outputs": [],
   "source": [
    "#检测下载文件夹内的文件数量是否变化\n",
    "def wenjian3():\n",
    "    if file2 > file:#如果输入验证码后的文件夹里文件数量增加，也就是数量大于输入前文件数量，则代表下载成功，验证码输入正确，返回True；反之则失败，输入错误，没有下载，文件数量没有大于前一次，返回False。\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 343,
   "id": "19b5e4a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def true():\n",
    "    qiehuan()\n",
    "    driver.close()\n",
    "    driver.switch_to.window(driver.window_handles[2])\n",
    "    time.sleep(1)\n",
    "    qiehuan()\n",
    "    driver.close()\n",
    "    driver.switch_to.window(driver.window_handles[1])\n",
    "    time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 344,
   "id": "5b05a91b",
   "metadata": {},
   "outputs": [
    {
     "ename": "NoSuchWindowException",
     "evalue": "Message: no such window: target window already closed\nfrom unknown error: web view not found\n  (Session info: chrome=90.0.4430.212)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNoSuchWindowException\u001b[0m                     Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-344-d543f99c6001>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      9\u001b[0m         \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0myzm_real\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m             \u001b[0melement\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'//*[@id=\"vImg\"]'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     12\u001b[0m             \u001b[0melement\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscreenshot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr\"F:\\screenImg.png\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m             \u001b[0mURL\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"http://webapi.xfyun.cn/v1/service/v1/ocr/general\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\aconda\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mfind_element_by_xpath\u001b[1;34m(self, xpath)\u001b[0m\n\u001b[0;32m    392\u001b[0m             \u001b[0melement\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'//div/td[1]'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    393\u001b[0m         \"\"\"\n\u001b[1;32m--> 394\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mby\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mBy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mXPATH\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mxpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    395\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    396\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mfind_elements_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mxpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\aconda\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mfind_element\u001b[1;34m(self, by, value)\u001b[0m\n\u001b[0;32m    974\u001b[0m                 \u001b[0mby\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mBy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mCSS_SELECTOR\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    975\u001b[0m                 \u001b[0mvalue\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'[name=\"%s\"]'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 976\u001b[1;33m         return self.execute(Command.FIND_ELEMENT, {\n\u001b[0m\u001b[0;32m    977\u001b[0m             \u001b[1;34m'using'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mby\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    978\u001b[0m             'value': value})['value']\n",
      "\u001b[1;32mF:\\aconda\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    319\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    320\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 321\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    322\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[0;32m    323\u001b[0m                 response.get('value', None))\n",
      "\u001b[1;32mF:\\aconda\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001b[0m in \u001b[0;36mcheck_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    240\u001b[0m                 \u001b[0malert_text\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    241\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malert_text\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 242\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    243\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    244\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNoSuchWindowException\u001b[0m: Message: no such window: target window already closed\nfrom unknown error: web view not found\n  (Session info: chrome=90.0.4430.212)\n"
     ]
    }
   ],
   "source": [
    "for page in range(1,4): #先进行三页循环，可自行选择爬取页数\n",
    "    for i in range(1,50):#一共抓取当前页面的50篇文章,i为每一篇的代号\n",
    "        pdf_xpath = '//*[@id=\"gridTable\"]/table/tbody/tr[{}]/td[2]/a'.format(i) #使用format(i)遍历循环点击页面的每一篇文章  \n",
    "        driver.find_element_by_xpath(pdf_xpath).click()#点击文章进入文章详情页\n",
    "        time.sleep(5)\n",
    "        driver.switch_to.window(driver.window_handles[2])#定位新窗口\n",
    "        time.sleep(5)\n",
    "        driver.find_element_by_xpath('//*[@id=\"pdfDown\"]').click()#点击下载\n",
    "        time.sleep(3)\n",
    "        if yzm_real() is True:\n",
    "            element=driver.find_element_by_xpath('//*[@id=\"vImg\"]')\n",
    "            element.screenshot(r\"F:\\screenImg.png\")\n",
    "            URL = \"http://webapi.xfyun.cn/v1/service/v1/ocr/general\"\n",
    "            APPID = \"3c822ee7\"\n",
    "            API_KEY = \"ae1dbd7fe29c714277f51eb9cb684313\"\n",
    "            with open(r\"F:\\screenImg.png\", 'rb') as f:\n",
    "                f1 = f.read()\n",
    "            f1_base64 = str(base64.b64encode(f1), 'utf-8')\n",
    "            data = {\n",
    "                'image': f1_base64\n",
    "            }\n",
    "            r = requests.post(URL, data=data, headers=getHeader())\n",
    "            result = str(r.content, 'utf-8')\n",
    "            yzm = r.json()['data']['block'][0]['line'][0]['word'][0]['content']\n",
    "            driver.find_element_by_xpath('//*[@id=\"vcode\"]').clear() #清空输入框\n",
    "            driver.find_element_by_xpath('//*[@id=\"vcode\"]').send_keys(yzm)#输入验证码框\n",
    "            wenjian1()#检测下载文件夹内的文件数量\n",
    "            file = wenjian1()\n",
    "            time.sleep(4)\n",
    "            driver.find_element_by_xpath('/html/body/div/form/dl/dd/button').click()#点击验证码提交\n",
    "            wenjian2()#检测点击下载后文件夹内的文件数量\n",
    "            file2 = wenjian2()\n",
    "            time.sleep(3)\n",
    "            wenjian3()#判断下载文件夹内的文件数量是否变化\n",
    "            yzm_again=wenjian3()\n",
    "            while yzm_again == False: ##文件数量没有增加，验证码验证失败，重新输入\n",
    "                driver.switch_to.window(driver.window_handles[3])\n",
    "                element=driver.find_element_by_xpath('//*[@id=\"vImg\"]')\n",
    "                element.screenshot(r\"F:\\screenImg.png\")\n",
    "                URL = \"http://webapi.xfyun.cn/v1/service/v1/ocr/general\"\n",
    "                APPID = \"3c822ee7\"\n",
    "                API_KEY = \"ae1dbd7fe29c714277f51eb9cb684313\"\n",
    "                with open(r\"F:\\screenImg.png\", 'rb') as f:\n",
    "                    f1 = f.read()\n",
    "                f1_base64 = str(base64.b64encode(f1), 'utf-8')\n",
    "                data = {\n",
    "                    'image': f1_base64\n",
    "                }\n",
    "                r = requests.post(URL, data=data, headers=getHeader())\n",
    "                result = str(r.content, 'utf-8')\n",
    "                yzm = r.json()['data']['block'][0]['line'][0]['word'][0]['content']\n",
    "                driver.find_element_by_xpath('//*[@id=\"vcode\"]').clear() #清空输入框\n",
    "                driver.find_element_by_xpath('//*[@id=\"vcode\"]').send_keys(yzm)#输入验证码框\n",
    "                wenjian1()#检测下载文件夹内的文件数量\n",
    "                file = wenjian1()\n",
    "                time.sleep(6)\n",
    "                driver.find_element_by_xpath('/html/body/div/form/dl/dd/button').click()#点击验证码提交\n",
    "                wenjian2()#检测点击下载后文件夹内的文件数量\n",
    "                file2 = wenjian2()\n",
    "                time.sleep(6)\n",
    "                wenjian3()#判断下载文件夹内的文件数量是否变化\n",
    "                yzm_again=wenjian3()\n",
    "                if yzm_again == True:#如果判断等于True，则代表输入正确下载成功\n",
    "                    break\n",
    "            true()\n",
    "            \n",
    "            \n",
    "        else: #没有验证码出现的情况\n",
    "            qiehuan_详情()\n",
    "            driver.close()\n",
    "            driver.switch_to.window(driver.window_handles[1])\n",
    "            time.sleep(3)\n",
    "    driver.find_element_by_xpath('//*[@id=\"PageNext\"]').click()#一页循环挖取结束，点击进入下一页\n",
    "    time.sleep(5)\n",
    "    \n",
    "            \n",
    "            \n",
    "            \n",
    "                \n",
    "\n",
    "            \n",
    "            \n",
    "            \n",
    "            \n",
    "            \n",
    "            \n",
    "        \n",
    "        \n",
    "        \n",
    "    \n",
    "            \n",
    "        \n",
    "            \n",
    "            \n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2e24334",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
